﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Security.Cryptography;
using System.Windows.Threading;
using System.Windows;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;

namespace WpfImageDuplicates
{
    [Serializable]
    public class Settings : INotifyPropertyChanged
    {
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        [NonSerialized]
        public static Settings Values;

        protected double _thumbWidth = 160D;
        protected double _thumbHeight = 120D;
        protected bool _wantFilterThumbnails;
        protected bool _active = true;
        protected bool _viewerIsTopmost = true;

        protected Rectangle _window1Size;

        public bool ViewerIsTopmost
        {
            get { return _viewerIsTopmost; }
            set
            {
                if (_viewerIsTopmost != value)
                {
                    _viewerIsTopmost = value;
                    OnPropertyChanged("ViewerIsTopmost");
                }
            }
        }

        public Rectangle Window1Size
        {
            get { return _window1Size; }
            set
            {
                if (_window1Size != value)
                {
                    _window1Size = value;
                    OnPropertyChanged("Window1Size");
                }
            }
        }

        public double ThumbWidth
        {
            get { return _thumbWidth; }
            set
            {
                if (_thumbWidth != value)
                {
                    _thumbWidth = value;
                    OnPropertyChanged("ThumbWidth");
                }
            }
        }

        public double ThumbHeight
        {
            get { return _thumbHeight; }
            set
            {
                if (_thumbHeight != value)
                {
                    _thumbHeight = value;
                    OnPropertyChanged("ThumbHeight");
                }
            }
        }

        public bool WantFilterThumbnails
        {
            get { return _wantFilterThumbnails; }
            set
            {
                if (_wantFilterThumbnails != value)
                {
                    _wantFilterThumbnails = value;
                    OnPropertyChanged("WantFilterThumbnails");
                }
            }
        }
        public static Settings LoadSettings(string path)
        {
            if (!File.Exists(path))
                return null;

            byte[] bytes = File.ReadAllBytes(path);
            var settings = bytes.DeserializeFromCompressedBinary() as Settings;
            return settings;
        }

        public void SaveSettings(string path)
        {
            byte[] bytes = this.SerializeToCompressedBinary();
            File.WriteAllBytes(path, bytes);
        }

        public bool Active
        {
            get
            {
                return _active;
            }
            set
            {
                if (_active != value)
                {
                    _active = value;
                    OnPropertyChanged("Active");
                }
            }
        }

        public void ApplySettings(Window1 mainWindow)
        {
            Window1._filterCollection.Active = Active;
            if (Window1Size != null)
            {
                mainWindow.Left = Window1Size.Left;
                mainWindow.Top = Window1Size.Top;
                mainWindow.Width = Window1Size.Width;
                mainWindow.Height = Window1Size.Height;
            }
        }

        public void ReadSettings(Window1 mainWindow)
        {
            Active = Window1._filterCollection.Active;
            Window1Size = new Rectangle((int)mainWindow.Left, (int)mainWindow.Top, (int)mainWindow.ActualWidth, (int)mainWindow.ActualHeight);
        }

        internal void SetThumbWidthAndHeight(double w, double h)
        {
            ThumbWidth = w;
            ThumbHeight = h;
        }
    }
}
